package org.theonept.travian.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.theonept.travian.util.ConnexionUtil;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Compte extends Entity implements Serializable {
	@DatabaseField(generatedId = true)
	private Long idCompte;
	@DatabaseField
	private String urlServeur;
	@DatabaseField
	private String username;
	@DatabaseField
	private String password;
	@DatabaseField
	private EnumRace race;
	@DatabaseField
	private boolean state;
	private List<Village> villages;
	private List<Message> messages;
	private List<Rapport> rapports;

	public Compte() {
		this.villages = new ArrayList<Village>();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public EnumRace getRace() {
		return race;
	}

	public void setRace(EnumRace race) {
		this.race = race;
	}

	public boolean isEtat(boolean forceConnect, boolean forceUpdate) {

		if (!forceUpdate) {
			return state;
		}
		boolean etatCalc;
		if (forceConnect) {
			if (ConnexionUtil.getEtat(this)) {
				etatCalc = true;
			} else {
				ConnexionUtil.connect(this);
				etatCalc = isEtat(false, false);

			}
		} else {
			etatCalc = isEtat(false, true);
		}
		setState(etatCalc);
		return etatCalc;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public String getUrlServeur() {
		return urlServeur;
	}

	public void setUrlServeur(String urlServeur) {
		this.urlServeur = urlServeur;
	}

	public List<Village> getVillages() {
		if (isEtat(true, true) && (villages == null || !isUpToDate())) {
			villages = Village.getVillages(this.getUrlServeur() + "dorf1.php", this);
			update();
		}
		return villages;
	}

	public void setVillages(List<Village> villages) {
		this.villages = villages;
	}

	public static void parseCompte(String page, Compte compte) {

		Document first = Jsoup.parse(page);

		compte.setRace(EnumRace.getRaceFromId(first.getElementById("sidebarBoxHero")
				.getElementsByAttributeValueStarting("class", "nation").get(0).attr("class")));

	}

	/**
	 * @return the messages
	 */
	public List<Message> getMessages() {
		if (isEtat(true, false)) {
			messages = Message.getMessages(this);
		}
		return messages;
	}

	public List<Message> getUnreadMessage() {
		getMessages();

		List<Message> messagesUnread = new ArrayList<Message>();
		if (messages != null) {
			for (Message message : messages) {
				if (!message.getRead()) {
					messagesUnread.add(message);
				}
			}
		}
		return messagesUnread;
	}

	/**
	 * @param messages
	 *            the messages to set
	 */
	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public void disconnect() {
		ConnexionUtil.disconnect(this);
		setState(false);

	}

	/**
	 * @return the idCompte
	 */
	public Long getIdCompte() {
		return idCompte;
	}

	/**
	 * @param idCompte
	 *            the idCompte to set
	 */
	public void setIdCompte(Long idCompte) {
		this.idCompte = idCompte;
	}

	/**
	 * @return the rapports
	 */
	public List<Rapport> getRapports() {

		if (isEtat(true, false)) {
			rapports = Rapport.getRapports(this);
		}
		return rapports;

	}

	/**
	 * @param rapports
	 *            the rapports to set
	 */
	public void setRapports(List<Rapport> rapports) {
		this.rapports = rapports;
	}
}
